<?php
/**
 * File containing the RandoDateProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "RandomDateProcessor"
 *
 * Processor which is used to process tags which implements random dates within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage DateProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class RandomDateProcessor
{
	private $text;
	
	/**
	 * The processor adds random generated dates and repeats the dates n times
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {randomDate:-} 2012-03-10
			 * {randomDate:/} 10/03/2012
			 * {randomDate:.} 10.03.2012
			 * {randomDate:}  10032012
			 * {randomDate:r} 20120310
			 * Random date with given layout -1 year +2 years
			 */
			if ($m[0] == "randomDate") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "dmY";
				} else if ($parameter[0] == "r") {
					$parameter[0] = "Ymd";
				} else if ($parameter[0] == "-") {
					$parameter[0] = "Y-m-d";
				} else if ($parameter[0] == "/") {
					$parameter[0] = "d/m/Y";
				} else if ($parameter[0] == ".") {
					$parameter[0] = "d.m.Y";
				} else {
					$parameter[0] = "Y-m-d";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				$replacement = date($parameter[0], rand(time()-60*60*24*360, time()+60*60*24*360*2));
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[1]), $this->text);
			}
			
			/**
			 * {randomDateSmallerAs:10,3,2012,-} 2012-03-08
			 * {randomDateSmallerAs:10,3,2012,/} 08/03/2012
			 * {randomDateSmallerAs:10,3,2012,.} 08.03.2012
			 * {randomDateSmallerAs:10,3,2012}  08032012
			 * {randomDateSmallerAs:10,3,2012,r} 20120308
			 * Random date with given layout which is smaller as the given date. The given smaler date is max 3 years smaller as the given date
			 */
			if ($m[0] == "randomDateSmallerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				$time = mktime(0, 0, 0, $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[3]) || $parameter[3] == "") {
					$parameter[3] = "dmY";
				} else if ($parameter[3] == "r") {
					$parameter[3] = "Ymd";
				} else if ($parameter[3] == "-") {
					$parameter[3] = "Y-m-d";
				} else if ($parameter[3] == "/") {
					$parameter[3] = "d/m/Y";
				} else if ($parameter[3] == ".") {
					$parameter[3] = "d.m.Y";
				} else {
					$parameter[3] = "Y-m-d";
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = 1;
				}
			
				$replacement = date($parameter[3], rand($time-60*60*24*360*3, $time));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[4]), $this->text);
			}
			
			/**
			 * {randomDateBiggerAs:10,3,2012,-} 2012-03-11
			 * {randomDateBiggerAs:10,3,2012,/} 11/03/2012
			 * {randomDateBiggerAs:10,3,2012,.} 11.03.2012
			 * {randomDateBiggerAs:10,3,2012,}  11032012
			 * {randomDateBiggerAs:10,3,2012,r} 20120311
			 * Random date with given layout which is bigger as the given date. The generated date is max 3 years bigger as the given date
			 */
			if ($m[0] == "randomDateBiggerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				$time = mktime(0, 0, 0, $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[3]) || $parameter[3] == "") {
					$parameter[3] = "dmY";
				} else if ($parameter[3] == "r") {
					$parameter[3] = "Ymd";
				} else if ($parameter[3] == "-") {
					$parameter[3] = "Y-m-d";
				} else if ($parameter[3] == "/") {
					$parameter[3] = "d/m/Y";
				} else if ($parameter[3] == ".") {
					$parameter[3] = "d.m.Y";
				} else {
					$parameter[3] = "Y-m-d";
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = 1;
				}
			
				$replacement = date($parameter[3], rand($time, $time+60*60*24*360*3));
				
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[4]), $this->text);
			}
			
			/**
			 * {randomDateRange:10,3,2012,20,3,2012,-} 2012-03-10
			 * {randomDateRange:10,3,2012,20,3,2012,/} 10/03/2012
			 * {randomDateRange:10,3,2012,20,3,2012,.} 10.03.2012
			 * {randomDateRange:10,3,2012,20,3,2012,}  10032012
			 * {randomDateRange:10,3,2012,20,3,2012,r} 20120310
			 * Random date with given layout. The date is between the two given dates
			 */
			if ($m[0] == "randomDateRange") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				$timeFrom = mktime(0, 0, 0, $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("d", time() + 31104000);
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("m", time() + 31104000);
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("Y", time() + 31104000);
				}
				
				$timeUntil = mktime(0, 0, 0, $parameter[4], $parameter[3], $parameter[5]);
				
				if (!isset($parameter[6]) || $parameter[6] == "") {
					$parameter[6] = "dmY";
				} else if ($parameter[6] == "r") {
					$parameter[6] = "Ymd";
				} else if ($parameter[6] == "-") {
					$parameter[6] = "Y-m-d";
				} else if ($parameter[6] == "/") {
					$parameter[6] = "d/m/Y";
				} else if ($parameter[6] == ".") {
					$parameter[6] = "d.m.Y";
				} else {
					$parameter[6] = "Y-m-d";
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = 1;
				}
			
				$replacement = date($parameter[6], rand($timeFrom, $timeUntil));
				
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[7]), $this->text);
			}
			
			/**
			 * {randomDateTime:-} 2012-03-10 13:40:00
			 * {randomDateTime:/} 10/03/2012 13:41:00
			 * {randomDateTime:.} 10.03.2012 13:42:00
			 * {randomDateTime:}  10032012134300
			 * {randomDateTime:r} 20120310134400
			 * Random date and time with given layout
			 */
			if ($m[0] == "randomDateTime") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "dmYHis";
				} else if ($parameter[0] == "r") {
					$parameter[0] = "YmdHis";
				} else if ($parameter[0] == "-") {
					$parameter[0] = "Y-m-d H:i:s";
				} else if ($parameter[0] == "/") {
					$parameter[0] = "d/m/Y H:i:s";
				} else if ($parameter[0] == ".") {
					$parameter[0] = "d.m.Y H:i:s";
				} else {
					$parameter[0] = "Y-m-d H:i:s";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				$replacement = date($parameter[0], rand(time()-60*60*24*360, time()+60*60*24*360*2));
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[1]), $this->text);
			}
			
			/**
			 * {randomDateTimeSmallerAs:10,3,2012,13,01,35,-} 2012-02-10 13:01:35
			 * {randomDateTimeSmallerAs:10,3,2012,13,01,35,/} 10/02/2012 13:03:04
			 * {randomDateTimeSmallerAs:10,3,2012,13,01,35,.} 10.02.2012 13:02:49
			 * {randomDateTimeSmallerAs:10,3,2012,13,01,35}  10022012130149
			 * {randomDateTimeSmallerAs:10,3,2012,13,01,35,r} 20120210130149
			 * Random date and time with given layout. Result is smaller as the given datetime (max 3 years)
			 */
			if ($m[0] == "randomDateTimeSmallerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$time = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || $parameter[6] == "") {
					$parameter[6] = "dmYHis";
				} else if ($parameter[6] == "r") {
					$parameter[6] = "YmdHis";
				} else if ($parameter[6] == "-") {
					$parameter[6] = "Y-m-d H:i:s";
				} else if ($parameter[6] == "/") {
					$parameter[6] = "d/m/Y H:i:s";
				} else if ($parameter[6] == ".") {
					$parameter[6] = "d.m.Y H:i:s";
				} else {
					$parameter[6] = "Y-m-d H:i:s";
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = 1;
				}
				
				$replacement = date($parameter[6], rand($time-60*60*24*360*3, $time));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[7]), $this->text);
			}
			
			/**
			 * {randomDateTimeBiggerAs:10,3,2012,13,1,35,-} 2012-04-10 13:01:35
			 * {randomDateTimeBiggerAs:10,3,2012,13,1,35,/} 10/04/2012 13:03:04
			 * {randomDateTimeBiggerAs:10,3,2012,13,1,35,.} 10.04.2012 13:02:49
			 * {randomDateTimeBiggerAs:10,3,2012,13,1,35}  10042012130149
			 * {randomDateTimeBiggerAs:10,3,2012,13,1,35,r} 20120410130149
			 * Random date and time with given layout which is bigger as the given datetime
			 */
			if ($m[0] == "randomDateTimeBiggerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$time = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || $parameter[6] == "") {
					$parameter[6] = "dmYHis";
				} else if ($parameter[6] == "r") {
					$parameter[6] = "YmdHis";
				} else if ($parameter[6] == "-") {
					$parameter[6] = "Y-m-d H:i:s";
				} else if ($parameter[6] == "/") {
					$parameter[6] = "d/m/Y H:i:s";
				} else if ($parameter[6] == ".") {
					$parameter[6] = "d.m.Y H:i:s";
				} else {
					$parameter[6] = "Y-m-d H:i:s";
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = 1;
				}
				
				$replacement = date($parameter[6], rand($time, $time+60*60*24*360*3));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[7]), $this->text);
			}
			
			/**
			 * {randomDateTimeRange:10,3,2012,13,01,35,20,3,2012,13,01,35,-} 2012-03-10 13:01:35
			 * {randomDateTimeRange:10,3,2012,13,01,35,20,3,2012,13,01,35,/} 10/03/2012 13:03:04
			 * {randomDateTimeRange:10,3,2012,13,01,35,20,3,2012,13,01,35,.} 10.03.2012 13:02:49
			 * {randomDateTimeRange:10,3,2012,13,01,35,20,3,2012,13,01,35,}  10032012130149
			 * {randomDateTimeRange:10,3,2012,13,01,35,20,3,2012,13,01,35,r} 20120310130149
			 * Random date and time with given layout, between the two given date times
			 */
			if ($m[0] == "randomDateTimeRange") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$timeFrom  = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || !is_numeric($parameter[6])) {
					$parameter[6] = date("d", time() + 31104000);
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = date("m", time() + 31104000);
				}
				
				if (!isset($parameter[8]) || !is_numeric($parameter[8])) {
					$parameter[8] = date("Y", time() + 31104000);
				}
				
				if (!isset($parameter[9]) || !is_numeric($parameter[9])) {
					$parameter[9] = date("H", time() + 31104000);
				}
				
				if (!isset($parameter[10]) || !is_numeric($parameter[10])) {
					$parameter[10] = date("i", time() + 31104000);
				}
				
				if (!isset($parameter[11]) || !is_numeric($parameter[11])) {
					$parameter[11] = date("s", time() + 31104000);
				}
				
				$timeUntil = mktime($parameter[9], $parameter[10], $parameter[11], $parameter[7], $parameter[6], $parameter[8]);
				
				if (!isset($parameter[12]) || $parameter[12] == "") {
					$parameter[12] = "dmYHis";
				} else if ($parameter[12] == "r") {
					$parameter[12] = "YmdHis";
				} else if ($parameter[12] == "-") {
					$parameter[12] = "Y-m-d H:i:s";
				} else if ($parameter[12] == "/") {
					$parameter[12] = "d/m/Y H:i:s";
				} else if ($parameter[12] == ".") {
					$parameter[12] = "d.m.Y H:i:s";
				} else {
					$parameter[12] = "Y-m-d H:i:s";
				}
				
				if (!isset($parameter[13]) || !is_numeric($parameter[13])) {
					$parameter[13] = 1;
				}
				
				$replacement = date($parameter[12], rand($timeFrom, $timeUntil));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[13]), $this->text);
			}
			
			/**
			 * {randomDateTimeLayout:Y-m} 2012-05
			 * Returns the actual date time in a given configuration which is given in the first parameter.
			 * For documentation see http://de.php.net/manual/de/function.date.php
			 */
			if ($m[0] == "randomDateTimeLayout") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "dmYHis";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				$replacement = date($parameter[0], rand(time()-60*60*24*360, time()+60*60*24*360*2));
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[1]), $this->text);
			}
			
			/**
			 * {randomDateTimeSmallerAs:10,3,2012,13,01,35,Y-m} 2012-05
			 * Returns the actual date time in a given configuration which is given in the first parameter.
			 * For documentation see http://de.php.net/manual/de/function.date.php
			 */
			if ($m[0] == "randomDateTimeLayoutSmallerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$time = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || $parameter[6] == "") {
					$parameter[6] = "dmYHis";
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = 1;
				}
				
				$replacement = date($parameter[6], rand($time-60*60*24*360*3, $time));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[7]), $this->text);
			}
			
			/**
			 * {randomDateTimeBiggerAs:10,3,2012,13,1,35,Y-m} 2012-05
			 * Returns the actual date time in a given configuration which is given in the first parameter.
			 * For documentation see http://de.php.net/manual/de/function.date.php
			 */
			if ($m[0] == "randomDateTimeLayoutBiggerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$time = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || $parameter[6] == "") {
					$parameter[6] = "dmYHis";
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = 1;
				}
				
				$replacement = date($parameter[6], rand($time, $time+60*60*24*360*3));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[7]), $this->text);
			}
			
			/**
			 * {randomDateTimeRange:10,3,2012,13,01,35,20,3,2012,13,01,35,Y-m} 2012-05
			 * Returns the actual date time in a given configuration which is given in the first parameter.
			 * For documentation see http://de.php.net/manual/de/function.date.php
			 */
			if ($m[0] == "randomDateTimeRangeLayout") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$timeFrom  = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || !is_numeric($parameter[6])) {
					$parameter[6] = date("d", time() + 31104000);
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = date("m", time() + 31104000);
				}
				
				if (!isset($parameter[8]) || !is_numeric($parameter[8])) {
					$parameter[8] = date("Y", time() + 31104000);
				}
				
				if (!isset($parameter[9]) || !is_numeric($parameter[9])) {
					$parameter[9] = date("H", time() + 31104000);
				}
				
				if (!isset($parameter[10]) || !is_numeric($parameter[10])) {
					$parameter[10] = date("i", time() + 31104000);
				}
				
				if (!isset($parameter[11]) || !is_numeric($parameter[11])) {
					$parameter[11] = date("s", time() + 31104000);
				}
				
				$timeUntil = mktime($parameter[9], $parameter[10], $parameter[11], $parameter[7], $parameter[6], $parameter[8]);
				
				if (!isset($parameter[12]) || $parameter[12] == "") {
					$parameter[12] = "dmYHis";
				} 
				
				if (!isset($parameter[13]) || !is_numeric($parameter[13])) {
					$parameter[13] = 1;
				}
				
				$replacement = date($parameter[12], rand($timeFrom, $timeUntil));
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[13]), $this->text);
			}
			
			/**
			 * {randomUnixTimestamp:} 123456789
			 * Random unix timestamp
			 */
			if ($m[0] == "randomUnixTimestamp") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = 1;
				}
				
				$replacement = rand(time()-60*60*24*360, time()+60*60*24*360*2);
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[0]), $this->text);
			}
			
			/**
			 * {randomUnixTimestampSmallerAs:10,3,2012,13,01,35} 123456789
			 * Random unix timestamp. Result is smaller as the given datetime (max 3 years)
			 */
			if ($m[0] == "randomUnixTimestampSmallerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$time = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || !is_numeric($parameter[6])) {
					$parameter[6] = 1;
				}
				
				$replacement = rand($time-60*60*24*360*3, $time);
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[6]), $this->text);
			}
			
			/**
			 * {randomUnixTimestampBiggerAs:10,3,2012,13,1,35} 123456789
			 * Random unix timestamp which is bigger as the given datetime
			 */
			if ($m[0] == "randomUnixTimestampBiggerAs") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$time = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || !is_numeric($parameter[6])) {
					$parameter[6] = 1;
				}
				
				$replacement = rand($time, $time+60*60*24*360*3);
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[6]), $this->text);
			}
			
			/**
			 * {randomUnixTimestampRange:10,3,2012,13,01,35,20,3,2012,13,01,35} 12345678
			 * Random unix timestamp, between the two given date times
			 */
			if ($m[0] == "randomUnixTimestampRange") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || !is_numeric($parameter[0])) {
					$parameter[0] = date("d");
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = date("m");
				}
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = date("Y");
				}
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = date("H");
				}
				
				if (!isset($parameter[4]) || !is_numeric($parameter[4])) {
					$parameter[4] = date("i");
				}
				
				if (!isset($parameter[5]) || !is_numeric($parameter[5])) {
					$parameter[5] = date("s");
				}
				
				$timeFrom  = mktime($parameter[3], $parameter[4], $parameter[5], $parameter[1], $parameter[0], $parameter[2]);
				
				if (!isset($parameter[6]) || !is_numeric($parameter[6])) {
					$parameter[6] = date("d", time() + 31104000);
				}
				
				if (!isset($parameter[7]) || !is_numeric($parameter[7])) {
					$parameter[7] = date("m", time() + 31104000);
				}
				
				if (!isset($parameter[8]) || !is_numeric($parameter[8])) {
					$parameter[8] = date("Y", time() + 31104000);
				}
				
				if (!isset($parameter[9]) || !is_numeric($parameter[9])) {
					$parameter[9] = date("H", time() + 31104000);
				}
				
				if (!isset($parameter[10]) || !is_numeric($parameter[10])) {
					$parameter[10] = date("i", time() + 31104000);
				}
				
				if (!isset($parameter[11]) || !is_numeric($parameter[11])) {
					$parameter[11] = date("s", time() + 31104000);
				}
				
				$timeUntil = mktime($parameter[9], $parameter[10], $parameter[11], $parameter[7], $parameter[6], $parameter[8]);
				
				if (!isset($parameter[12]) || !is_numeric($parameter[12])) {
					$parameter[12] = 1;
				}
				
				$replacement = rand($timeFrom, $timeUntil);
					
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[12]), $this->text);
			}
		}
		
		return $this->text;
	}
	
	/**
	 * Manages the repeation of values
	 * 
	 * @param String $value,  the value which should be replaced
	 * @param String $string, the string for the cockie
	 * @param String $times,  the number of times the value should be repeated
	 * 
	 * @return String
	 */
	public function manageRepeation($value, $string, $times) {
		$replacement = "";
		
		if (Yii::app()->session[$string . "_times"] >= $times || !isset(Yii::app()->session[$string . "_times"])) {
			Yii::app()->session[$string . "_replacement"] = $value;
			Yii::app()->session[$string . "_times"] = 1;
							
			$replacement = $value;
		} else {
			$number = Yii::app()->session[$string . "_replacement"];
			$times = Yii::app()->session[$string . "_times"] + 1;
			
			Yii::app()->session[$string . "_times"] = $times;
			
			$replacement = $number;
		}
		
		Yii::app()->session['resetManager']->add($string . "_replacement");
		Yii::app()->session['resetManager']->add($string . "_times");
		
		return $replacement;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{randomDate:format}', 'Random date with given layout -1 year +2 years'),
			array('{randomDateSmallerAs:day,month,year,format}', 'Random date with given layout which is smaller as the given date. The given smaler date is max 3 years smaller as the given date'),
			array('{randomDateBiggerAS:day,month,year,format}', 'Random date with given layout which is bigger as the given date. The generated date is max 3 years bigger as the given date'),  
			array('{randomDateRange:fromDay,fromMonth,fromYear,untilDay,untilMonth,untilYear,format}', 'Random date with given layout. The date is between the two given dates'),
			array('{randomDateTime:format}', 'Random date and time with preconfigured layout'),
			array('{randomDateTimeSmallerAs:day,month,year,hour,minute,second,format}', 'Random date and time with given layout. Result is smaller as the preconfigured datetime (max 3 years)'),
			array('{randomDateTimeBiggerAs:day,month,year,hour,minute,second,format}', 'Random date and time with given layout which is bigger as the preconfigured datetime'),
			array('{randomDateTimeRange:fromDay,fromMonth,fromYear,fromHour,fromMinute,fromSecond,untilDay,untilMonth,untilYear,untilHour,untilMinute,untilSecond,format}', 'Random date and time with preconfigured layout, between the two given date times'),
			array('{randomDateTimeLayout:format}', 'Random date and time with given layout'),
			array('{randomDateTimeLayoutSmallerAs:day,month,year,hour,minute,second,format}', 'Random date and time with given layout. Result is smaller as the given datetime (max 3 years)'),
			array('{randomDateTimeLayoutBiggerAs:day,month,year,hour,minute,second,format}', 'Random date and time with given layout which is bigger as the given datetime'),
			array('{randomDateTimeRangeLayout:fromDay,fromMonth,fromYear,fromHour,fromMinute,fromSecond,untilDay,untilMonth,untilYear,untilHour,untilMinute,untilSecond,format}', 'Random date and time with given layout, between the two given date times'),
			array('{randomUnixTimestamp:}', 'Random unix timestamp'),
			array('{randomTimestampSmallerAs:day,month,year,hour,minute,second}', 'Random unix timestamp. Result is smaller as the given datetime (max 3 years)'),
			array('{randomTimestampBiggerAs:day,month,year,hour,minute,second}', 'Random unix timestamp which is bigger as the given datetime'),
			array('{randomTimestampRange:fromDay,fromMonth,fromYear,fromHour,fromMinute,fromSecond,untilDay,untilMonth,untilYear,untilHour,untilMinute,untilSecond}', 'Random unix timestamp, between the two given date times'),
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return string
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
}
?>